package com.danci.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danci.common.Result;
import com.danci.dto.LexiconDto;
import com.danci.dto.UserRequest;
import com.danci.entity.Lexicon;
import com.danci.mapper.LexiconMapper;
import com.danci.service.ILexiconService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 词库主表 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-05-13
 */
@Service
@AllArgsConstructor
public class LexiconServiceImpl extends ServiceImpl<LexiconMapper, Lexicon> implements ILexiconService {

    private final LexiconMapper lexiconMapper;

    @Override
    public Result getLexiconList(UserRequest userRequest) {
        List<LexiconDto> lexiconList = lexiconMapper.getLexiconList(userRequest.getUserId());
        return Result.success(lexiconList);
    }

    @Override
    public Result insertLexicon(Lexicon lexicon) {
        QueryWrapper<Lexicon> wrapper = new QueryWrapper<>();
        wrapper.eq("name", lexicon.getName());
        Lexicon selectOne = lexiconMapper.selectOne(wrapper);
        if (selectOne != null) {
            return Result.fail("词库名称重复");
        } else {
            String id = IdUtil.fastSimpleUUID();
            lexicon.setId(id);
            lexiconMapper.insert(lexicon);
            return Result.success(id);
        }
    }
}
